package com.mailbox.mailbox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mailbox.mailbox.domain.Message;
import com.mailbox.mailbox.domain.Reply;
import com.mailbox.mailbox.domain.User;
import com.mailbox.mailbox.domain.responsePojo.ReturnReply;
import com.mailbox.mailbox.mapper.MessageMapper;
import com.mailbox.mailbox.mapper.ReplyMapper;
import com.mailbox.mailbox.mapper.UserMapper;
import com.mailbox.mailbox.service.ReplyService;
import com.mailbox.mailbox.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 栗守佳
* @description 针对表【reply】的数据库操作Service实现
* @createDate 2022-05-18 17:27:19
*/
@Service
@Slf4j
public class ReplyServiceImpl extends ServiceImpl<ReplyMapper, Reply>
    implements ReplyService {

    @Resource
    UserMapper userMapper;
    @Resource
    MessageMapper messageMapper;
    @Resource
    ReplyMapper replyMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult replyMessage(Integer userId, String replyContent,Integer messageId) {
        User user= userMapper.selectById(userId);
        if(user==null){
            return ResponseResult.error("该用户不存在");
        }
        Message message=messageMapper.selectById(messageId);
        if(message==null){
            return ResponseResult.error("该留言不存在");
        }
        QueryWrapper<Reply> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("message_id",messageId);
        long count=this.count(queryWrapper);
        System.out.println("count"+count);
        if(count>=1){
            log.info("你已经回复过了");
            return ResponseResult.error("你已经回复过了");
        }

        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date1=simpleDateFormat.format(date);
        Reply reply=new Reply();
        reply.setReplyContent(replyContent);
        reply.setMessageId(messageId);
        reply.setReplyContent(replyContent);
        reply.setCreateTime(date1);
        reply.setReplyUserId(userId);
        Boolean b =this.save(reply);
        if(b){
            UpdateWrapper<Message> messageUpdateWrapper=new UpdateWrapper<>();
            messageUpdateWrapper.eq("message_id",messageId);
            Message message1=new Message();
            message1.setIsReply(1);
            messageMapper.update(message1,messageUpdateWrapper);
            return ResponseResult.success("回复成功",reply);
        }else{
            return ResponseResult.error("服务器繁忙,请稍后重试");
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateReply(Integer replyId, String content) {
        Reply reply=this.getById(replyId);
        if(reply==null){
            return ResponseResult.error("该回复不存在");
        }
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("reply_id",replyId);
        Reply reply1=new Reply();
        reply1.setReplyContent(content);
        Boolean b=  this.update(reply1,updateWrapper);
        if(b){
            return ResponseResult.success("修改成功");
        }else{
            return ResponseResult.error("服务器繁忙,请稍后重试");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteReply(Integer replyId) {
        Reply reply=this.getById(replyId);
        if(reply==null){
            return ResponseResult.error("该回复不存在");
        }
        Boolean b=this.removeById(replyId);
        if(b){
            UpdateWrapper<Message> messageUpdateWrapper=new UpdateWrapper<>();
            messageUpdateWrapper.eq("message_id",reply.getMessageId());
            Message message1=new Message();
            message1.setIsReply(2);
            message1.setIsOpen(0);
            messageMapper.update(message1,messageUpdateWrapper);
            return ResponseResult.success("删除成功");
        }else{
            return ResponseResult.error("服务器繁忙,请稍后重试");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult batchDeleteReply(List<Integer> replyId) {

            for(int x:replyId){
                Reply reply=this.getById(x);
                if(reply==null){
                    continue;
                }else{
                    UpdateWrapper<Message> messageUpdateWrapper=new UpdateWrapper<>();
                    messageUpdateWrapper.eq("message_id",reply.getMessageId());
                    Message message1=new Message();
                    message1.setIsReply(2);
                    message1.setIsOpen(0);
                    messageMapper.update(message1,messageUpdateWrapper);
                }
            }
            Boolean b=this.removeBatchByIds(replyId);
        if(b){
            return ResponseResult.success("删除成功");
        }else{
            return ResponseResult.error("服务器繁忙,请稍后重试");
        }
    }

    @Override
    public ResponseResult findAllReply(Integer pageNow, Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List<ReturnReply> replyList= replyMapper.findAllReplays();
        PageInfo<ReturnReply> pageInfo=new PageInfo<>(replyList);
        return ResponseResult.success("查询成功",pageInfo);
    }

    @Override
    public ResponseResult findReplyByNameLike(Integer pageNow, Integer pageSize,String keyWord) {
        PageHelper.startPage(pageNow,pageSize);
        List<ReturnReply> replyList= replyMapper.findReplyByNameLike(keyWord);
        PageInfo<ReturnReply> pageInfo=new PageInfo<>(replyList);
        return ResponseResult.success("查询成功",pageInfo);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult findReplyByMessageId(Integer messageId) {
        Message message=messageMapper.selectById(messageId);
        if(message==null){
            return ResponseResult.error("该留言不存在");
        }
        if(message.getIsReply()==0){
            //将未查看状态变为未回复
            UpdateWrapper<Message> mw=new UpdateWrapper<>();
            mw.set("is_reply",2);
            mw.eq(" message_id",messageId);
            messageMapper.update(new Message(),mw);
        }
        if(message.getIsReply()!=1){
            return ResponseResult.success("该留言未回复");
        }
        Reply reply=replyMapper.findDetailsReply(messageId);
        String nickname=userMapper.selectById(reply.getReplyUserId()).getNickname();
        ReturnReply returnReply =new ReturnReply();
        returnReply.setReplyId(reply.getReplyId());
        returnReply.setNickname(nickname);
        returnReply.setMessageId(messageId);
        returnReply.setReplyContent(reply.getReplyContent());
        returnReply.setCreateTime(reply.getCreateTime());
        returnReply.setReplyUserId(reply.getReplyUserId());
        return ResponseResult.success("查询成功", returnReply);
    }
}




